package GoldMiner.com.ALi;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

/**
 * @author ：ALi
 * @date ：Created in 2021/10/31 9:56
 * @description：划线的类
 * @modified By：
 * @version: $
 */
public class Line {
    //起点坐标
    private int x = 380;
    private int y = 180;

    //终点坐标
    private int endx = 500;
    private int endy = 500;
    //设置线的长度
    private double length = 100;
    //线的角度(角度在0~π之间，用angle*π来定角度)
    private double angle = 0;
    //判断方向的参数
    private int dir = 1;
    //判断红线的状态 0 左右摇摆，1 抓取 2 收回 不能设置为private，在GameWin中更改了值
    protected int state;
    //主窗口元素.用来判定抓取金块
    protected GameWin frame;
    //狗爪图片
    protected static Image hook = Toolkit.getDefaultToolkit().getImage("imgs/hook.png");
    //设置线的最大最小长度
    protected double min_length = 100;
    protected double max_length = 750;

    //构造函数
    Line(GameWin frame){
        this.frame = frame;
    }

    /**
     * create by: ALi
     * description: 判断红线是否碰到了金块
     * create time: 2021/10/31 10:56
     * @Param: null
     * @return
     */
    void logic(){
        //遍历多个金块进行判断
        for (Object obj:this.frame.objectList){
            if (endx > obj.x && endx < obj.x + obj.width
                    && endy > obj.y && endy < obj.y + obj.height){
                state = 3;
                obj.flag = true;
            }
        }

    }

    /**
     * create by: ALi
     * description: 画线的方法
     * create time: 2021/10/31 10:26
     * @Param: null
     * @return
     */
    void drawline(Graphics graphics){
        endx = (int)(x + length*Math.cos(angle*Math.PI));
        endy = (int)( y + length*Math.sin(angle*Math.PI));
        graphics.setColor(Color.red);
        //将线加粗
        graphics.drawLine(x-1,y-1,endx-1,endy);
        graphics.drawLine(x,y,endx,endy);
        graphics.drawLine(x+1,y+1,endx+1,endy);

        graphics.drawImage(hook,endx-36,endy-2,null);
    }

    /**
     * create by: ALi
     * description: 按照鼠标点击事件选择画线的方式:0 左右摇摆，1 抓取 2 收回 3抓取返回(有速度差别)
     * create time: 2021/10/31 10:27
     *
     * @Param: null
     * @return
     */
    void paintLine(Graphics graphics){
        logic();
        //0 左右摇摆，1 抓取 2 收回
        switch (state){
            case 0:
                //设置角度
                if(angle < 0.1 ){
                    dir = 1;
                }else if(angle > 0.9){
                    dir = -1;
                }
                angle = angle + 0.005*dir;
                drawline(graphics);
                break;
            case 1:
                //线的长度小于500则延长
                if(length < max_length){
                    length = length + 10;
                    drawline(graphics);
                }else{
                    state = 2;
                }
                break;
            case 2:
                //收回线长变小
                if(length > min_length){
                    length =length - 10;
                    drawline(graphics);
                }else{
                    state = 0;
                }
                break;
            case 3:
                int m = 1;//接收金块或石块的质量
                //遍历多个金块
                //碰到金块，收回线长变小
                if(length > min_length){
                        for (Object obj:this.frame.objectList){
                            if(obj.flag == true){
                                m = obj.m;
                                length =length - 10;
                                drawline(graphics);
                                obj.x = endx - obj.getWidth()/2;
                                obj.y = endy;
                                if (length <= min_length){
                                    obj.x = -150;
                                    obj.y = -150;
                                    obj.flag = false;
                                    //计算积分
                                    BackGroundImgs.count+=obj.count;
                                    state = 0;
                                }
                                if(BackGroundImgs.WaterFlag == true){
                                    //glod
                                    if(obj.type == 1){
                                        m=1;
                                        BackGroundImgs.WaterFlag = false;
                                    }
                                    //rock
                                    if(obj.type == 2){
                                        obj.x = -150;
                                        obj.y = -150;
                                        obj.flag = false;
                                        BackGroundImgs.WaterFlag = false;
                                        state = 2;
                                    }
                                }
                            }

                        }

                }
                try {
                    Thread.sleep(m);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            default:
        }


    }
    /**
     * create by: ALi
     * description: 重置线的元素
     * create time: 2021/10/31 16:24
     *
      * @Param: null
     * @return
     */
    public void reGame(){
        angle = 0;
        length = 100;

    }

}